package _base0to1;

/**
 * 28. 找出字符串中第一个匹配项的下标
 */
public class No28 {
    public int strStr(String haystack, String needle) {
        int n = haystack.length();
        int m = needle.length();
        if (n < m) return -1;
        int left = 0, right = 0;
        while (right <= n - m) {
            for (char c : needle.toCharArray()) {
                if (haystack.charAt(right) != c) break;
                else right++;
            }
            if (right - left == m) return left;
            else right = ++left;
        }
        return -1;
    }
}
